package com.study.spark.scala.chapter07

import scala.io.{BufferedSource, Source}

object Scala05_Collection_3{
  def main(args: Array[String]): Unit = {
    // TODO 集合 - 功能函数 - WordCount案列 - 扩展:不规则数据
    val list = List(
      ("Hello World",3),
      ("Hello Spark",2)
    )
    println(list)//List((Hello World,3), (Hello Spark,2))

    // TODO 对于原始数据不是规则的数据处理
    // 思路一：将数据变回原本规则的格式
    // Hello World Hello World Hello World
    // Hello Spark Hello Spark

    val list1 = list.map(
      str => {
        val line = str._1
        val cnt = str._2
        (line + " ") * cnt
      }
    )
    println(list1)
    //List(Hello World Hello World Hello World , Hello Spark Hello Spark )

    val wordcount =
      list1
        .flatMap(_.split(" "))
        .groupBy(str => str)
        .mapValues(_.size)
    println(wordcount)//Map(Hello -> 5, Spark -> 2, World -> 3)


  }
}
